package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.system.UserBoatOrderInfo;
import com.ruoyi.common.enums.OrderStatus;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.mapper.UserBoatOrderInfoMapper;
import com.ruoyi.system.service.IUserBoatOrderInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 船只与订单信息关联Service业务层处理
 *
 * @author zjw
 * @date 2023-08-29
 */
@Slf4j
@Service
public class UserBoatOrderInfoServiceImpl implements IUserBoatOrderInfoService {
    @Resource
    private UserBoatOrderInfoMapper userBoatOrderInfoMapper;

    /**
     * 查询船只与订单信息关联
     *
     * @param boatId 船只与订单信息关联主键
     * @return 船只与订单信息关联
     */
    @Override
    public UserBoatOrderInfo selectBoatOrderInfoByBoatId(Long boatId) {
        return userBoatOrderInfoMapper.selectBoatOrderInfoByBoatId(boatId);
    }

    /**
     * 查询船只与订单信息关联列表
     *
     * @param userBoatOrderInfo 船只与订单信息关联
     * @return 船只与订单信息关联
     */
    @Override
    public List<UserBoatOrderInfo> selectBoatOrderInfoList(UserBoatOrderInfo userBoatOrderInfo) {
        return userBoatOrderInfoMapper.selectBoatOrderInfoList(userBoatOrderInfo);
    }

    /**
     * 新增船只与订单信息关联
     *
     * @param userBoatOrderInfo 船只与订单信息关联
     * @return 结果
     */
    @Override
    public int insertBoatOrderInfo(UserBoatOrderInfo userBoatOrderInfo) {
        return userBoatOrderInfoMapper.insertBoatOrderInfo(userBoatOrderInfo);
    }

    /**
     * 修改船只与订单信息关联
     *
     * @param userBoatOrderInfo 船只与订单信息关联
     * @return 结果
     */
    @Override
    public int updateBoatOrderInfo(UserBoatOrderInfo userBoatOrderInfo) {
        return userBoatOrderInfoMapper.updateBoatOrderInfo(userBoatOrderInfo);
    }

    /**
     * 批量删除船只与订单信息关联
     *
     * @param boatIds 需要删除的船只与订单信息关联主键
     * @return 结果
     */
    @Override
    public int deleteBoatOrderInfoByBoatIds(Long[] boatIds) {
        return userBoatOrderInfoMapper.deleteBoatOrderInfoByBoatIds(boatIds);
    }

    /**
     * 删除船只与订单信息关联信息
     *
     * @param boatId 船只与订单信息关联主键
     * @return 结果
     */
    @Override
    public int deleteUserBoatOrderInfoByUserId(Long boatId) {
        return userBoatOrderInfoMapper.deleteUserBoatOrderInfoByUserId(boatId);
    }

    @Override
    public List<UserBoatOrderInfo> selectUserBoatOrderInfoListByBoatIdentityId(String boatIdentityId) {
        return userBoatOrderInfoMapper.selectUserBoatOrderInfoListByBoatIdentityId(boatIdentityId);
    }

    /**
     * 根据船只出厂id查找到订单状态为"使用中"的UserBoatOrderInfo对象。如果有多个的话则返回第一个。
     *
     * @param boatIdentityId
     * @return
     */
    public UserBoatOrderInfo getUserBoatOrderInfoOfUsingStatusByBoatIdentityId(String boatIdentityId) {
        //根据boatIdentityId查找到当前处于待使用的订单
        List<UserBoatOrderInfo> userBoatOrderInfoList = selectUserBoatOrderInfoListByBoatIdentityId(boatIdentityId);

        if (Objects.isNull(userBoatOrderInfoList) || userBoatOrderInfoList.size() == 0) {
            log.warn("当前船只此时没有对应的UserBoatOrderInfo对象，因此此时也没有使用中的订单。");
            return null;
        }

        //查找出订单状态为"使用中"的UserBoatOrderInfo对象
        List<UserBoatOrderInfo> userBoatOrderInfos = userBoatOrderInfoList.stream().filter(item ->
                item.getOrderStatus() == OrderStatus.Using.getCode()).collect(Collectors.toList());

        if (userBoatOrderInfos.size() == 0) {
            log.warn("当前船只此时没有对应的使用中的订单!");
            return null;
        } else if (userBoatOrderInfos.size() > 1) {
            log.warn("当前船只此时对应的使用中的订单大于或等于2个，此情况异常，需检查!");
            return userBoatOrderInfos.get(0);
        } else {
            return userBoatOrderInfos.get(0);
        }
    }

    @Override
    public UserBoatOrderInfo selectUserBoatOrderInfoByUserId(Long userId) {
        return userBoatOrderInfoMapper.selectBoatOrderInfoByUserId(userId);
    }
}
